package com.darren.euc.action;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.darren.euc.entity.*;
import com.darren.euc.service.MsgService;
import com.darren.euc.util.Kv;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.darren.euc.service.MessageVisitService;
import com.darren.euc.service.impl.MessageClientEndServiceImpl;
import com.darren.euc.util.PropertyHelper;

import net.sf.json.JSONObject;


/**
 * Created by IIS72013 on 2018/4/21.
 */
@Controller
@RequestMapping("/message")
public class MessageAction {
	
    private static final Logger log = Logger.getLogger(MessageAction.class);

    @Autowired
    private MessageVisitService messageVisitService;
    
    @Autowired
    private MessageClientEndServiceImpl messageClientEndServiceImpl;

    @Autowired
    private MsgService msgService;

    /**
     * 设备预约参见消息已读
     * @param requestJson
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value= "/readvisit",method = RequestMethod.POST)
    public ResponseJSON readVisit(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
    	ResponseJSON responseJson = new ResponseJSON();
    	try{
    		Integer id = requestJson.getInt("id");
        	MessageVisit messageVisit = messageVisitService.findById(id);    	
        	messageVisit.setMessageStatus(1);
        	messageVisit.setReadTime(new Date());
        	messageVisit = messageVisitService.update(messageVisit);    	
        	responseJson.setStatus(0);
        	responseJson.setMessage("修改成功！");
        	responseJson.setData(messageVisit);
    	}catch(Exception ex){
    		log.error(ex.getMessage(),ex);
    		responseJson.setStatus(-1);
        	responseJson.setMessage("修改失败！");
    	}
    	return responseJson;
    }
    
    
    @ResponseBody
    @RequestMapping(value= "/readclientend",method = RequestMethod.POST)
    public ResponseJSON readClientEnd(@RequestBody JSONObject requestJson, HttpServletResponse response, HttpServletRequest request){
    	ResponseJSON responseJson = new ResponseJSON();
    	try{
    		Integer id = requestJson.getInt("id");
        	MessageClientEnd messageClientEnd = messageClientEndServiceImpl.findById(id);    	
        	messageClientEnd.setMessageStatus(1);
        	messageClientEnd.setReadTime(new Date());
        	messageClientEnd = messageClientEndServiceImpl.update(messageClientEnd);    	
        	responseJson.setStatus(0);
        	responseJson.setMessage("修改成功！");
        	responseJson.setData(messageClientEnd);
    	}catch(Exception ex){
    		log.error(ex.getMessage(),ex);
    		responseJson.setStatus(-1);
        	responseJson.setMessage("修改失败！");
    	}
    	return responseJson;
    }
    
    
    


    /**
     * 查询预约参观消息
     * @param requestJson
     * @param request
     * @param response
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ResponseBody
    @RequestMapping(value = "/query/messagevisit",method = RequestMethod.POST)
    public ResponseJSON queryMessageVisit(@RequestBody JSONObject requestJson, HttpServletRequest request, HttpServletResponse response) {
        ResponseJSON responseJson = new ResponseJSON();
        User user = (User) request.getAttribute("user");
        if(user != null){
        	requestJson.getJSONObject("data").put("userId", user.getId());
        	PageBean pageBean = messageVisitService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
            responseJson.setData(pageBean);
            responseJson.setStatus(0);
            responseJson.setMessage("查询成功！");
        }else{
    		responseJson.setStatus(-1);
        	responseJson.setMessage("查询失败！");
        }
        return responseJson;
    }

    /**
     * 查询预约参观消息
     * @param requestJson
     * @param request
     * @param response
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ResponseBody
    @RequestMapping(value = "/query/messageclientend",method = RequestMethod.POST)
    public ResponseJSON queryMessageClientEnd(@RequestBody JSONObject requestJson, HttpServletRequest request, HttpServletResponse response) {
        ResponseJSON responseJson = new ResponseJSON();
        User user = (User) request.getAttribute("user");
        if(user != null){
        	requestJson.getJSONObject("data").put("userId", user.getId());
        	PageBean pageBean = messageClientEndServiceImpl.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
            responseJson.setData(pageBean);
            responseJson.setStatus(0);
            responseJson.setMessage("查询成功！");
        }else{
    		responseJson.setStatus(-1);
        	responseJson.setMessage("查询失败！");
        }
        return responseJson;
    }

    /**
     * 查询预约参观消息
     * @param requestJson
     * @param request
     * @param response
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "/query/unread",method = RequestMethod.POST)
    public Map<String,Object> queryUnreadMessageCount(HttpServletRequest request, HttpServletResponse response) {
    	Map<String,Object> result = new HashMap<String,Object>();
        User user = (User) request.getAttribute("user");
        if(user != null){
        	BigInteger visitCount = messageVisitService.getUnreadCount(user.getId());
        	BigInteger clientEndCount = messageClientEndServiceImpl.getUnreadCount(user.getId());
        	result.put("visitMessage", visitCount);
        	result.put("clientEndMessage", clientEndCount);
        }
        return result;
    }

    /**
     * 查询站内短信
     * @param request
     * @param pn
     * @param ps
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query/usermsg",method = RequestMethod.GET)
    public ResponseJSON queryMessageCount(HttpServletRequest request,
                                          @RequestParam(name = "pn",required = false, defaultValue = "1") int pn,
                                          @RequestParam(name = "ps",required = false, defaultValue = "10") int ps) {
        User user = (User) request.getAttribute("user");
        if (user == null){
            return new ResponseJSON(-3,"登录信息有误",null);
        }
        PageBean pageBean = msgService.msgListForWeb(user,pn,ps);
        return new ResponseJSON().setData(pageBean);
    }

    /**
     * 消息详情
     */
    @ResponseBody
    @RequestMapping(value = "/query/msg_detail")
    public ResponseJSON msgDetail(HttpServletRequest request,
                          @RequestParam(name = "message_id") int message_id){

        Map map = msgService.msgInfo(message_id);
        User user = (User) request.getAttribute("user");
        //记录阅读记录
        if (user != null){
            msgService.msgLogSaveForWeb(user.getId(), message_id);
        }
        return new ResponseJSON().setData(map);
    }

    /**
     * 消息统计
     */
    @ResponseBody
    @RequestMapping(value = "/query/msg_count")
    public ResponseJSON msgCount(HttpServletRequest request){
        ResponseJSON bean = new ResponseJSON();
        User user = (User) request.getAttribute("user");

        //未登录直接返回
        if (user == null){
            return bean.setData(Kv.of("count", 0));
        }

        Kv kv = Kv.of();
        kv.set("user", user);
        Map map = msgService.msgCountForWeb(kv);
        return bean.setData(map);
    }

    /**
     * 一键全看
     */
    @ResponseBody
    @RequestMapping(value = "/query/readed_all")
    public ResponseJSON readedAll(HttpServletRequest request){
        ResponseJSON bean = new ResponseJSON();
        User user = (User) request.getAttribute("user");

        //未登录直接返回
        if (user == null){
            return bean.setData(Kv.of("count", 0));
        }

        Kv kv = Kv.of();
        kv.set("user", user);
        msgService.readedAllForWeb(kv);
        return new ResponseJSON();
    }

}
